#include "quote_manager.h"
#include <algorithm>

namespace yquote {

QuoteManager::QuoteManager() = default;

QuoteManager::~QuoteManager() = default;

void QuoteManager::updateQuote(const QuoteData& quote) {
    {
        std::lock_guard<std::mutex> lock(quotes_mutex_);
        
        // 如果是新股票，直接添加
        auto it = quotes_.find(quote.symbol);
        if (it == quotes_.end()) {
            quotes_[quote.symbol] = quote;
        } else {
            // 更新现有股票数据，计算涨跌幅
            QuoteData& existing = it->second;
            double prev_close = existing.close > 0 ? existing.close : existing.price;
            
            existing.price = quote.price;
            existing.high = std::max(existing.high, quote.price);
            existing.low = existing.low > 0 ? std::min(existing.low, quote.price) : quote.price;
            existing.volume += quote.volume;
            existing.timestamp = quote.timestamp;
            
            // 计算涨跌额和涨跌幅
            if (prev_close > 0) {
                existing.change = quote.price - prev_close;
                existing.change_percent = (existing.change / prev_close) * 100.0;
            }
        }
    }
    
    // 通知订阅者
    notifySubscribers(quote);
}

QuoteData QuoteManager::getQuote(const std::string& symbol) const {
    std::lock_guard<std::mutex> lock(quotes_mutex_);
    
    auto it = quotes_.find(symbol);
    if (it != quotes_.end()) {
        return it->second;
    }
    
    return QuoteData(); // 返回空的QuoteData
}

std::vector<QuoteData> QuoteManager::getAllQuotes() const {
    std::lock_guard<std::mutex> lock(quotes_mutex_);
    
    std::vector<QuoteData> result;
    result.reserve(quotes_.size());
    
    for (const auto& pair : quotes_) {
        result.push_back(pair.second);
    }
    
    return result;
}

bool QuoteManager::hasSymbol(const std::string& symbol) const {
    std::lock_guard<std::mutex> lock(quotes_mutex_);
    return quotes_.find(symbol) != quotes_.end();
}

void QuoteManager::subscribe(const std::string& client_id, const std::vector<std::string>& symbols) {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    for (const auto& symbol : symbols) {
        addSubscription(client_id, symbol);
    }
}

void QuoteManager::unsubscribe(const std::string& client_id, const std::vector<std::string>& symbols) {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    for (const auto& symbol : symbols) {
        removeSubscription(client_id, symbol);
    }
}

void QuoteManager::unsubscribeAll(const std::string& client_id) {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    auto it = client_subscriptions_.find(client_id);
    if (it != client_subscriptions_.end()) {
        // 从反向索引中移除
        for (const auto& symbol : it->second) {
            auto symbol_it = symbol_subscribers_.find(symbol);
            if (symbol_it != symbol_subscribers_.end()) {
                symbol_it->second.erase(client_id);
                if (symbol_it->second.empty()) {
                    symbol_subscribers_.erase(symbol_it);
                }
            }
        }
        
        // 移除客户端订阅
        client_subscriptions_.erase(it);
    }
}

std::vector<std::string> QuoteManager::getSubscriptions(const std::string& client_id) const {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    auto it = client_subscriptions_.find(client_id);
    if (it != client_subscriptions_.end()) {
        return std::vector<std::string>(it->second.begin(), it->second.end());
    }
    
    return std::vector<std::string>();
}

void QuoteManager::setQuoteUpdateCallback(QuoteUpdateCallback callback) {
    quote_update_callback_ = std::move(callback);
}

std::vector<std::string> QuoteManager::getSubscribers(const std::string& symbol) const {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    auto it = symbol_subscribers_.find(symbol);
    if (it != symbol_subscribers_.end()) {
        return std::vector<std::string>(it->second.begin(), it->second.end());
    }
    
    return std::vector<std::string>();
}

size_t QuoteManager::getQuoteCount() const {
    std::lock_guard<std::mutex> lock(quotes_mutex_);
    return quotes_.size();
}

size_t QuoteManager::getSubscriptionCount() const {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    
    size_t count = 0;
    for (const auto& pair : client_subscriptions_) {
        count += pair.second.size();
    }
    return count;
}

size_t QuoteManager::getClientCount() const {
    std::lock_guard<std::mutex> lock(subscriptions_mutex_);
    return client_subscriptions_.size();
}

void QuoteManager::notifySubscribers(const QuoteData& quote) {
    if (quote_update_callback_) {
        quote_update_callback_(quote);
    }
}

void QuoteManager::addSubscription(const std::string& client_id, const std::string& symbol) {
    // 添加到客户端订阅列表
    client_subscriptions_[client_id].insert(symbol);
    
    // 添加到反向索引
    symbol_subscribers_[symbol].insert(client_id);
}

void QuoteManager::removeSubscription(const std::string& client_id, const std::string& symbol) {
    // 从客户端订阅列表中移除
    auto client_it = client_subscriptions_.find(client_id);
    if (client_it != client_subscriptions_.end()) {
        client_it->second.erase(symbol);
        if (client_it->second.empty()) {
            client_subscriptions_.erase(client_it);
        }
    }
    
    // 从反向索引中移除
    auto symbol_it = symbol_subscribers_.find(symbol);
    if (symbol_it != symbol_subscribers_.end()) {
        symbol_it->second.erase(client_id);
        if (symbol_it->second.empty()) {
            symbol_subscribers_.erase(symbol_it);
        }
    }
}

} // namespace yquote 